const pattern = {
  mobie: /^((\+?86)|(\(\+86\)))?(13[012356789][0-9]{8}|15[012356789][0-9]{8}|17[012356789][0-9]{8}|19[012356789][0-9]{8}|18[02356789][0-9]{8}|147[0-9]{8}|1349[0-9]{7})$/,
  english: /^[a-zA-Z]+$/,
  englishAndNumber: /^[a-zA-Z0-9]+$/
}

/**
 * 邮箱
 * @param str
 */
export function isEmail (str) {
  return /^([a-zA-Z0-9_-])+@([a-zA-Z0-9_-])+((.[a-zA-Z0-9_-]{2,3}){1,2})$/.test(str)
}

/**
 * 手机号码
 * @param str
 */
export function isMobile (str) {
  return pattern.mobie.test(str)
}

/**
 * 电话号码
 * @param str
 */
export function isPhone (str) {
  return /^([0-9]{3,4}-)?[0-9]{7,8}$/.test(str)
}

export function validate (rule, value) {
  if (rule == null) return '';
  let tempValue = rule.transform ? rule.transform(value) : value;
  if (rule.required && (tempValue == null || tempValue === '')) {
    return rule.message || '不能为空!';
  }
  if (rule.pattern && !rule.pattern.test(tempValue)) {
    return rule.message || '格式错误!';
  }
  switch (rule.type) {
    case 'email':
      if (!isEmail(tempValue)) {
        return rule.message || '邮箱格式错误!';
      }
      break;
    case 'integer':
      if (!/^-?\d+$/.test(tempValue)) {
        return rule.message || '请输入整数!';
      }
      break;
    case 'number':
      if (!/^-?\d+$/.test(tempValue)) {
        return rule.message || '请输入数字!';
      } else {
        if (rule.min && tempValue < rule.min) {
          return rule.message || '数值太小!';
        }
        if (rule.max && tempValue > rule.max) {
          return rule.message || '数值太大!';
        }
      }
      break;
    case 'string':
      if (rule.min && tempValue.length < rule.min) {
        return rule.message || '长度不足!';
      }
      if (rule.max && tempValue.length > rule.max) {
        return rule.message || '超出长度!';
      }
      break;
    default:
      break;
  }
  return '';
}

export function validateWidget(rules, value) {
  return new Promise((resolve, reject) => {
    if (Array.isArray(rules)) {
      for (let i = 0; i < rules.length; i++) {
        let rule = rules[i];
        let errorMessage = validate(rule, value);
        if (errorMessage !== '') {
          reject(errorMessage);
          return;
        }
      }
    }
    resolve();
  });

}

export const widgetRuleMixin = {
  props: {
    rules: {
      type: Array,
      default: () => []
    }
  },
  data() {
    return {
      errorMessage: ''
    }
  },
  methods: {
    validate() {
      return new Promise((resolve, reject) => {
        if (Array.isArray(this.rules)) {
          for (let i = 0; i < this.rules.length; i++) {
            let rule = this.rules[i];
            this.errorMessage = validate(rule, this.value);
            if (this.errorMessage !== '') {
              reject(this.errorMessage);
              return;
            }
          }
          this.errorMessage = '';
          resolve();
        }
      });
    },
    resetField() {
      this.errorMessage = '';
    }
  }
}

export const formRuleMixin = {
  data() {
    return {
      widgetImplList: []
    }
  },
  methods: {
    validateForm() {
      return new Promise((resolve, reject) => {
        if (Array.isArray(this.widgetImplList)) {
          let tempWidgetList = this.widgetImplList.filter(widget => {
            return widget && widget.$refs.widgetImpl && widget.$refs.widgetImpl.validateWidget;
          });
          let tempList = tempWidgetList.map(widget => {
            return widget.$refs.widgetImpl.validateWidget();
          });
          Promise.all(tempList).then(res => {
            console.log(res);
            res.filter(item => item !== '' && item != null).length > 0 ? reject() : resolve();
          }).catch(e => {
            reject(e);
          });
				} else {
          resolve();
        }
      });
    }
  }
}

export default {
  pattern
}
